<?php
require 'inc_security.php';
$record_id = getValue('record_id');
$whs_supplies_id = getValue("whs_supplies_id","int","POST",0);
$whs_supplier_id = getValue("whs_supplier_id","int","POST",0);
$whs_warehouse_id = getValue("whs_warehouse_id","int","POST",0);
$whs_supplies_price = getValue("whs_supplies_price","int","POST",0);
$whs_supplies_number = getValue("whs_supplies_number","int","POST",0);
$whs_supplies_price_total = $whs_supplies_price*$whs_supplies_number;
$whs_supplies_name = $list_supplies[$whs_supplies_id];
$myform = new generate_form();
$myform->add('whs_supplies_id','whs_supplies_id',0,0,'',1,'Bạn chưa nhập tên vật tư');
$myform->add('whs_supplies_name','whs_supplies_name',0,1,'',0,'Bạn chưa nhập tên vật tư');
$myform->add('whs_supplies_number','whs_supplies_number',1,0,0);
$myform->add('whs_supplies_price','whs_supplies_price',1,0,0);
$myform->add('whs_supplies_price_total','whs_supplies_price_total',1,1,0);
$myform->add('whs_supplier_id','whs_supplier_id',1,0,0);
$myform->add('whs_warehouse_id','whs_warehouse_id',1,0,0);
$myform->addTable($bg_table);
$myform->removeHTML(0);
$action = getValue('action','str','POST','');

//lấy dữ liệu record cần sửa đổi
$db_data 	= new db_query("SELECT * FROM " . $bg_table . " WHERE " . $id_field . " = " . $record_id);
if($row 		= mysql_fetch_assoc($db_data->result)){
	foreach($row as $key=>$value){
		$$key = $value;
	}
}else{
		exit();
}

if($action == 'execute'){
    $bg_errorMsg .= $myform->checkdata();
    if($bg_errorMsg == ''){
        $db_insert = new db_execute($myform->generate_update_SQL($id_field,$record_id));
        redirect('listing.php');
    }
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="vi" lang="vi" xmlns:og="http://ogp.me/ns#" xmlns:fb="https://www.facebook.com/2008/fbml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <?=$load_header?>
</head>
<body>
<div class="module_header bold fix"><?=$module_name?></div>
<div id="wrapper">
    <?print_error_msg($bg_errorMsg)?>
    <?php $form = new form();
    ?>
    <?=$form->form_open()?>
    <?=$form->textnote('Các trường có dấu (<span class="form-asterick">*</span>) là bắt buộc nhập')?>
    <?//=$form->text(array('label'=>'Mã vật từ','name'=>'whs_supplies_code','id'=>'whs_supplies_code','require'=>0,'errorMsg'=>'Bạn chưa nhập mã vật tư'))?>
	<?=$form->select(array('label'=>'Vật tư','name'=>'whs_supplies_id', 'id'=>'whs_supplies_id','option'=>$list_supplies, 'title'=>'Chọn kho','require'=>1,'errorMsg'=>'Bạn chưa chọn tên vật tư','selected'=>$whs_supplies_id))?>
	<?=$form->text(array('label'=>'Số lượng','name'=>'whs_supplies_number','id'=>'whs_supplies_number','value'=>$whs_supplies_number))?>
	<?=$form->text(array('label'=>'Đơn giá','name'=>'whs_supplies_price','id'=>'whs_supplies_price','value'=>$whs_supplies_price))?>
   <?=$form->select(array('label'=>'Nhà cung cấp','name'=>'whs_supplier_id', 'id'=>'whs_supplier_id','option'=>$list_supplier, 'title'=>'Chọn Nhà cung cấp','require'=>1,'errorMsg'=>'Bạn chưa chọn nhà cung cấp','selected'=>$whs_supplier_id))?>
   <?
      if($isAdmin || $warehouse_id == 0) echo $form->select(array('label'=>'Kho','name'=>'whs_warehouse_id', 'id'=>'whs_warehouse_id','option'=>$list_wah, 'title'=>'Chọn kho','require'=>1,'errorMsg'=>'Bạn chưa chọn kho','selected'=>$whs_warehouse_id));
      else {
   ?>
         <input type="hidden" value="<?=$warehouse_id?>" name="whs_warehouse_id"/>
   <?
      }
   ?>
	<?=$form->form_action(array('label'=>array('Cập nhật','Nhập lại'),'type'=>array('submit','reset')))?>
	<?=$form->form_close()?>
</div>
</body>
</html>